<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class FiledController extends Controller
{
    //
    public function getFiled(){
        try{

            $chose = DB::table('filed')->get();

            return view('Admin.filed')->with('results',$chose);

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }
    public function updateFiled(){
        try{

            $data  = Request::only('filed_id','filed_name');

            $validator = Validator::make($data, [
                'filed_id' => 'required',
                'filed_name' => 'required',
            ]);

            if ($validator->fails()) {
                return $validator->messages();
            }
            $chose = DB::table('filed')->where('filed_id','=',$data['filed_id'])->update($data);

            return redirect('/admin/getFiled');

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }
}
